package com.rst.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rst.model.Classe;
import com.rst.model.Section;
import com.rst.model.Student;
import com.rst.model.StudentCategory;
import com.rst.model.UserDetailsDTO;

@Repository
@Transactional
public class StudentDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public List<Student> getAllStudent()
	{
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Student.class);
		return criteria.list();
	}

	public Long save(Student student)
	{
		return (Long) sessionFactory.getCurrentSession().save(student);
	}

	public void udateStudentCategory(StudentCategory category) {
		 sessionFactory.getCurrentSession().update(category);
	}

	public Long saveCategory(StudentCategory category) {
		return (Long) sessionFactory.getCurrentSession().save(category);
	}

	public StudentCategory getStudentCategoryById(Long categoryId) {
	    Session session = sessionFactory.getCurrentSession();
	    StudentCategory studentCategory= (StudentCategory)session.get(StudentCategory.class, categoryId);
		return studentCategory;

	}

	@SuppressWarnings("unchecked")
	public List<Section> getClassAndSections() {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Section sec WHERE sec.status = :status";
	    Query query = session.createQuery(str);
	    query.setParameter("status",  Section.ACTIVE);
	    
	    List<Section> secList = query.list(); 
	    for (Section section : secList) {
	    	 Hibernate.initialize(section.getClas());
		}
	    return query.list(); 

		
	}

	@SuppressWarnings("unchecked")
	public List<StudentCategory> getStudentCategory() {
	
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM StudentCategory c WHERE c.status = :status ORDER BY c.name ";
	    	
	    Query query = session.createQuery(str);
	    query.setParameter("status",  Classe.ACTIVE);
		return query.list();
	}
	
	

	public Student getAdmissionNo() {
	    Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Student s order by admissionNo desc limit 1";
	    Query query = session.createQuery(str);
	    query.setMaxResults(1);
		return (Student) query.uniqueResult();
	
	}

	public void saveStudent(Student student) {
		sessionFactory.getCurrentSession().save(student);		
	}

	public void saveUser(UserDetailsDTO userDetails) {
		sessionFactory.getCurrentSession().save(userDetails);				
	}

	@SuppressWarnings("unchecked")
	public List<Student> getStudentsBySectionId(Long sectionId) {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Student std WHERE std.sectionId = :sectionId  AND std.status = :status";
	    Query query = session.createQuery(str);
	    query.setParameter("status",  Student.ACTIVE);
	    query.setParameter("sectionId",  sectionId);
	    List<Student> stdList = query.list(); 
	    for (Student student : stdList) {
	    	 Hibernate.initialize(student.getSections().getClas());
	    	 Hibernate.initialize(student.getStudentCategory());
	    	 Hibernate.initialize(student.getUserDetails());
	    	 Hibernate.initialize(student.getAttendance());
		}
	    return query.list(); 
	}

	@SuppressWarnings("unchecked")
	public List<UserDetailsDTO> getStudentsByName(String name) {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM UserDetailsDTO ud WHERE ud.userDisplayNameFirst LIKE :name ";
	    Query query = session.createQuery(str);
	    query.setParameter("name",  "%" + name + "%");
	    return query.list(); 
	}

	public Student getStudentByStudentId(Long studentId) {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Student std WHERE std.id = :studentId AND std.status = :status";
	    Query query = session.createQuery(str);
	    query.setParameter("studentId",  studentId);
	    query.setParameter("status",  Student.ACTIVE);
	    Student student = (Student) query.uniqueResult();
	   	    	 Hibernate.initialize(student.getSections().getClas());
	   	    	 Hibernate.initialize(student.getStudentCategory());
	   	    	 Hibernate.initialize(student.getUserDetails());
	   	    	 Hibernate.initialize(student.getAttendance());
		return (Student) query.uniqueResult();
	}

	public Student getStudentByAdmNo(Long admissionNo) {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Student std WHERE std.admissionNo = :admissionNo AND std.status = :status";
	    Query query = session.createQuery(str);
	    query.setParameter("admissionNo",  admissionNo);
	    query.setParameter("status",  Student.ACTIVE);
	    Student student = (Student) query.uniqueResult();
	   	    	 Hibernate.initialize(student.getSections().getClas());
	   	    	 Hibernate.initialize(student.getStudentCategory());
	   	    	 Hibernate.initialize(student.getUserDetails());
	   	    	 Hibernate.initialize(student.getAttendance());
		return (Student) query.uniqueResult();
	}

}
